﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public class XeDAO : DataProvider
    {

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            XeDTO objectDTO = new XeDTO();
            objectDTO.MaXe = int.Parse(dt.Rows[i]["MaXe"].ToString());
            objectDTO.BienSoXe = dt.Rows[i]["BienSoXe"].ToString();
            objectDTO.LoaiXe = dt.Rows[i]["LoaiXe"].ToString();
            objectDTO.HangSanXuat = dt.Rows[i]["HangSanXuat"].ToString();
            objectDTO.NgayMua = DateTime.Parse(dt.Rows[i]["NgayMua"].ToString());
            objectDTO.KichThuoc = dt.Rows[i]["KichThuoc"].ToString();
            objectDTO.TheTich = float.Parse(dt.Rows[i]["TheTich"].ToString());
            objectDTO.TrongLuong = float.Parse(dt.Rows[i]["TrongLuong"].ToString());
            objectDTO.TinhTrang = dt.Rows[i]["TinhTrang"].ToString();
            objectDTO.TongGioVanChuyen = dt.Rows[i]["TongGioVanChuyen"].ToString();
            objectDTO.TongSoKmDaDi = dt.Rows[i]["TongSoKmDaDi"].ToString();

            return (object)objectDTO;
        }

        public XeDTO[] GetAll()
        {
            String strSQL = "Select * from Xe";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);

            XeDTO[] XeDTOs = new XeDTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                XeDTOs[i] = (XeDTO)GetDataFromDataRow(dt, i);

            return XeDTOs;
        }
        public void insert(XeDTO info)
        {


            string insertCommand = "INSERT Xe VALUES(" +
                info.MaXe + ", '" +
                info.BienSoXe + ", '" +
                info.LoaiXe + ", '" +
                info.HangSanXuat + ", '" +
                info.NgayMua + ", '" +
                info.KichThuoc + ", '" +
                info.TheTich + ", '" +
                info.TrongLuong + ", '" +
                info.TinhTrang + ", '" +
                info.TongGioVanChuyen + ", '" +
                info.TongSoKmDaDi + "')";

            ExecuteNonQuery(insertCommand);
        }

        public void update(XeDTO info)
        {
            string updateCommand = "UPDATE XE " +
                                    "SET BienSo = '" + info.BienSoXe + "', " +
                                    " LoaiXe = " + info.LoaiXe +
                                    " HangSanXuat = " + info.HangSanXuat +
                                    " NgayMua = " + info.NgayMua +
                                    " KichThuoc = " + info.KichThuoc +
                                    " TheTich = " + info.TheTich +
                                    " TrongLuong = " + info.TrongLuong +
                                    " TinhTrang = " + info.TinhTrang +
                                    " TongGioVanChuyen = " + info.TongGioVanChuyen +
                                    " TongSoKmDaDi = " + info.TongSoKmDaDi +
                                    " WHERE MaXe = " + info.MaXe;

            ExecuteNonQuery(updateCommand);
        }
    }
}
